<template>
  <div class="permission">
    <el-card>
      <div style="text-align:right">
        <el-button type="primary" size="small" @click="addCila(1, '0')"
          >添加权限</el-button
        >
      </div>
    </el-card>
    <el-card>
      <el-table :data="list" border row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" align="center" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              @click="addCila(1, row.id)"
              >添加页面</el-button
            >
            <el-button
              v-if="row.type === 1"
              type="text"
              @click="addCila(2, row.id)"
              >添加按钮</el-button
            >
            <el-button type="text" @click="edit(row)">编辑</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <Add ref="add" v-model="show" @getData="getData" />
  </div>
</template>

<script>
import { sysPermissIon, sysPermissIonDelete } from '@/api/permission'
import { changeData } from '@/utils/index'
import Add from './commit/add.vue'
export default {
  components: { Add },
  data () {
    return {
      list: [],
      show: false
    }
  },
  created () {
    this.getData()
  },
  methods: {
    // 导入页面调用api
    async getData () {
      const res = await sysPermissIon()
      this.list = changeData(res.data.data, '0')
      console.log('查看数据', res)
    },
    //  新增点击
    addCila (type, pid) {
      this.show = true
      this.$refs.add.form.type = type
      this.$refs.add.form.pid = pid
      this.$refs.add.form.mode = 'add'
    },
    // 删除事件
    del (id) {
      this.$confirm('你确定要删除嘛？')
        .then(async () => {
          await sysPermissIonDelete(id)
          this.$message.success('删除成功')
          this.getData()
        })
        .catch(() => {})
    },
    // 编辑事件
    edit (row) {
      this.show = true
      //  修改mode的值
      this.$refs.add.mode = 'edit'
      //  数据回显
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      console.log('row', row)
    }
  }
}
</script>

<style lang="scss" scoped>
.permission {
  padding: 20px;
}
</style>
